package com.netintech.intelligenceInterface.iflytek.controller;

import com.netintech.intelligenceInterface.iflytek.domain.semanticsStructured.SemanticsStructuredParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import static com.netintech.intelligenceInterface.common.StaticBean.URL2;


/**
 * @author lining
 * @date 2021/4/28 8:59
 * 语义结构化接口
 */
@RestController
public class SemanticsStructuredController {
    @Autowired
    private RestTemplate restTemplate;

    private static final String SEMANTICS_STRUCTURED_URL = "/semcmd/api/v1/semcmd/parse/";

    @GetMapping("/smanticsStructured/{trackId}")
    public String semanticsStructured(@PathVariable String trackId, SemanticsStructuredParam param) {
        return getSemanticsStructuredResult(trackId, param);
    }

    @GetMapping("/test/smanticsStructured/{trackId}")
    public String semanticsStructuredTest(@PathVariable String trackId, SemanticsStructuredParam param) {
        return getSemanticsStructuredResult(trackId, param);
    }

    private String getSemanticsStructuredResult(String trackId, SemanticsStructuredParam param) {
        String url = URL2 + SEMANTICS_STRUCTURED_URL + trackId;
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        String url2 = builder.queryParam("text", param.getText())
                .queryParam("sceneKey", param.getSceneKey())
                .queryParam("uid", param.getUid())
                .queryParam("stype", param.getStype())
                .queryParam("wtime", param.getWtime())
                .queryParam("force", param.getForce())
                .queryParam("props", param.getProps())
                .build().encode().toString();
        ResponseEntity<String> response = restTemplate.getForEntity(url2, String.class);
        return response.getBody();
    }
}


